package com.grow.greenhouse.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.DatePrinter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.grow.greenhouse.common.ErrorCode;
import com.grow.greenhouse.domain.ExceptionVO;
import com.grow.greenhouse.domain.Historyexception;
import com.grow.greenhouse.domain.User;
import com.grow.greenhouse.exception.BusinessException;
import com.grow.greenhouse.service.HistoryexceptionService;
import com.grow.greenhouse.mapper.HistoryexceptionMapper;
import com.grow.greenhouse.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Collectors;

/**
* @author 23854
* @description 针对表【historyexception(历史异常信息)】的数据库操作Service实现
* @createDate 2024-01-29 12:34:52
*/
@Service
@Slf4j
public class HistoryexceptionServiceImpl extends ServiceImpl<HistoryexceptionMapper, Historyexception>
    implements HistoryexceptionService{

    @Resource
    private UserService userService;

    @Resource
    private Gson gson;

    @Override
    public List<ExceptionVO> getException(HttpServletRequest request) {
        QueryWrapper<Historyexception> queryWrapper = getHistoryexceptionQueryWrapper(request);
        queryWrapper.last("limit 3");
        return getExceptionVOS(queryWrapper);
    }

    @Override
    public List<ExceptionVO> getAllException(HttpServletRequest request) {
        QueryWrapper<Historyexception> queryWrapper = getHistoryexceptionQueryWrapper(request);
        return getExceptionVOS(queryWrapper);
    }

    @NotNull
    private List<ExceptionVO> getExceptionVOS(QueryWrapper<Historyexception> queryWrapper) {
        return this.list(queryWrapper).stream().map(item -> {
            ExceptionVO exceptionVO = new ExceptionVO();
            BeanUtils.copyProperties(item, exceptionVO);
            String format = DateUtil.format(item.getUpdateTime(), "yyyy-MM-dd HH:mm:ss");
            exceptionVO.setUpdateTime(format);
            return exceptionVO;
        }).collect(Collectors.toList());
    }


    private QueryWrapper<Historyexception> getHistoryexceptionQueryWrapper(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        QueryWrapper<Historyexception> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.orderByDesc("updateTime");
        return queryWrapper;
    }
}




